<?php

class Result {

    public $id;
    public $result;
    public $result_ms;
    public $distance;
    public $stroke;
    public $team;
    public $seed;
    public $fina;
    public $date;
    public $exhibition;
    public $athlete_id;
    public $relay_id;
    public $event_id;
    public $competition_id;
    public $split_id;
    //not database fields
    public $athleteName;
    public $athleteAge;
    public $eventRaw;
    public $relay;

    function __construct($result = NULL, $result_ms = 0, $distance = NULL, $stroke = NULL, 
            $team = NULL, $seed = NULL, $fina = NULL, $date = '', $exhibition = 0, $athlete_id = 0, $relay_id = 0, $event_id = 0, $competition_id = 0) {
        $this->result = $result;
        $this->result_ms = $result_ms;
        $this->distance = $distance;
        $this->stroke = $stroke;
        $this->team = $team;
        $this->seed = $seed;
        $this->fina = $fina;
        $this->date = $date;
        $this->exhibition = $exhibition;
        $this->athlete_id = $athlete_id;
        $this->relay_id = $relay_id;
        $this->event_id = $event_id;
        $this->competition_id = $competition_id;
    }

    /**
     * Load Result from db by ID.
     * @param int $id
     * @return Result
     */
    public static function fromID($id) {
        $instance = new self();
        $instance->loadByID($id);
        return $instance;
    }

    /**
     * Load Result from database row.
     * @param array $row
     * @return Result
     */
    public static function fromRow(array $row) {
        $instance = new self();
        $instance->fill($row);
        return $instance;
    }

    private function loadByID($id) {
        $result = mysql_query("SELECT `id`, `result`, `result_ms`, `distance`, `stroke`, `team`, `seed`, `fina`, `date`, `exhibition`, `athlete_id`, `relay_id`, `event_id`, `competition_id`, `split_id` FROM `results` WHERE `id`='$id'");
        if (mysql_num_rows($result) == 0) {
            return null;
        }
        $row = mysql_fetch_assoc($result);
        $this->fill($row);
    }

    private function fill(array $row) {
        $this->id = $row['id'];
        $this->result = $row['result'];
        $this->result_ms = $row['result_ms'];
        $this->distance = $row['distance'];
        $this->stroke = $row['stroke'];
        $this->team = $row['team'];
        $this->seed = $row['seed'];
        $this->fina = $row['fina'];
        $this->date = $row['date'];
        $this->exhibition = $row['exhibition'];
        $this->athlete_id = $row['athlete_id'];
        $this->relay_id = $row['relay_id'];
        $this->event_id = $row['event_id'];
        $this->competition_id = $row['competition_id'];
        $this->split_id = $row['split_id'];
    }

    public function toArray() {
        return array($this->result, $this->result_ms, $this->distance, $this->stroke, $this->team, $this->seed, $this->fina, $this->date, $this->exhibition, $this->athlete_id, $this->relay_id, $this->event_id, $this->competition_id, $this->split_id);
    }

    public function getID() {
        return $this->id;
    }

    public function getResult() {
        return $this->result;
    }

    public function setResult($result) {
        $this->result = $result;
    }

    public function getResultMS() {
        return $this->result_ms;
    }

    public function setResultMS($result_ms) {
        $this->result_ms = $result_ms;
    }

    public function getDistance() {
        return $this->distance;
    }

    public function setDistance($distance) {
        $this->distance = $distance;
    }

    public function getStroke() {
        return $this->stroke;
    }

    public function setStroke($stroke) {
        $this->stroke = $stroke;
    }

    public function getTeam() {
        return $this->team;
    }

    public function setTeam($team) {
        $this->team = $team;
    }

    public function getSeed() {
        return $this->seed;
    }

    public function setSeed($seed) {
        $this->seed = $seed;
    }

    public function getFina() {
        return $this->fina;
    }

    public function setFina($fina) {
        $this->fina = $fina;
    }

    public function getAthleteID() {
        return $this->athlete_id;
    }

    public function setAthleteID($athlete_id) {
        $this->athlete_id = $athlete_id;
    }

    public function getRelayID() {
        return $this->relay_id;
    }

    public function setRelayID($relay_id) {
        $this->relay_id = $relay_id;
    }

    public function getEventID() {
        return $this->event_id;
    }

    public function setEventID($event_id) {
        $this->event_id = $event_id;
    }

    public function getCompetitionID() {
        return $this->competition_id;
    }

    public function setCompetitionID($competition_id) {
        $this->competition_id = $competition_id;
    }

    public function getDate() {
        return $this->date;
    }

    public function setDate($date) {
        $this->date = $date;
    }
    
    public function getExhibition() {
        return $this->exhibition;
    }

    public function setExhibition($exhibition) {
        $this->exhibition = $exhibition;
    }

    public function getSplitID() {
        return $this->split_id;
    }

    public function setSplitID($split_id) {
        $this->split_id = $split_id;
    }

    
    /**
     * Returns Relay associated with the Result.
     * @return Relay
     */
    public function getRelay() {
        return $this->relay;
    }

    public function setRelay(Relay $relay) {
        $this->relay = $relay;
    }

    public function getEventRaw() {
        return $this->eventRaw;
    }

    public function setEventRaw($eventRaw) {
        $this->eventRaw = $eventRaw;
    }
    public function getAthleteName() {
        return $this->athleteName;
    }

    public function setAthleteName($athleteName) {
        $this->athleteName = $athleteName;
    }

    public function getAthleteAge() {
        return $this->athleteAge;
    }

    public function setAthleteAge($athleteAge) {
        $this->athleteAge = $athleteAge;
    }

    
    public function __toString() {
        $toString = '';
        $toString .= 'result: ' . $this->result;
        $toString .= ', result_ms: ' . $this->result_ms;
        $toString .= ', distance: ' . $this->distance;
        $toString .= ', stroke: ' . $this->stroke;
        $toString .= ', team: ' . $this->team;
        $toString .= ', seed: ' . $this->seed;
        $toString .= ', fina: ' . $this->fina;
        $toString .= ', date: ' . $this->date;
        $toString .= ', exhibition: ' . $this->exhibition;
        $toString .= ', athlete_id: ' . $this->athlete_id;
        $toString .= ', relay_id: ' . $this->relay_id;
        $toString .= ', event_id: ' . $this->event_id;
        $toString .= ', competition_id: ' . $this->competition_id;
        $toString .= ', split_id: ' . $this->split_id;
        if ($this->relay != NULL) {
            $toString .= '<br>' . $this->relay;
        }
        $toString .= '<br>';
        return $toString;
    }

    public function toTableRow() {
        return "<tr><td>$this->result</td><td>$this->result_ms</td><td>$this->stroke</td><td>$this->distance</td></tr>";
    }

    public function toTableColumns() {
        return "<td>$this->result</td><td>$this->result_ms</td><td>$this->stroke</td><td>$this->distance</td>";
    }

}

?>
